package com.gdit.zax.utils;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.AudioInfo;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.MultimediaInfo;
import it.sauronsoftware.jave.VideoAttributes;
import it.sauronsoftware.jave.VideoInfo;
import it.sauronsoftware.jave.VideoSize;

import java.io.File;

import com.gdit.zax.Constant;

public class FFMPEtool {
	public static final int ERROR_NULL = -1;
	public static final int ERROR_NOEXIST_sourcefile = 2;
	public static final int ERROR_NOEXIST_targetpic = 3;
	public static final int ERROR_Encoder = 4;
	public static final int ERROR_YOUCODE = 5;
	
	//转换到可以播放的视频文件default_h263_mp4
	public static int geth263video (String sourcefile,String targetpic) {
		File source = null;
		File target = null;
		
		try {
			source = new File(sourcefile);
			target = new File(targetpic);
			
			Encoder encoder = new Encoder();
			
			MultimediaInfo info = encoder.getInfo(source);
			VideoInfo vinfo = info.getVideo();
			
			AudioAttributes audio = new AudioAttributes();
			audio.setCodec("libfaac");
			audio.setBitRate(new Integer(128000));
			audio.setSamplingRate(new Integer(44100));
			audio.setChannels(new Integer(2));
		
			VideoAttributes video = new VideoAttributes();
			video.setCodec("h263");
			video.setBitRate(new Integer(160000));
			video.setFrameRate(new Integer(15));
//			video.setSize(new VideoSize(176, 144));
			video.setSize(new VideoSize(704, 576));
//			video.setSize(vinfo.getSize());
			
			EncodingAttributes attrs = new EncodingAttributes();
			attrs.setFormat("mp4");
			attrs.setAudioAttributes(audio);
			attrs.setVideoAttributes(video);
			

//		

//			System.out.println(vinfo.getSize());
			
			
//			AudioAttributes audio = new AudioAttributes();
//			audio.setCodec("mp3");
//			
//			VideoAttributes video = new VideoAttributes();
//			video.setCodec("h263");
//			video.setBitRate(vinfo.getBitRate());
//			video.setFrameRate((int)vinfo.getFrameRate());
//			System.out.println(vinfo.getSize());
//			video.setSize(vinfo.getSize());
//			
//			EncodingAttributes attrs = new EncodingAttributes();
//			attrs.setFormat("mov");
//			attrs.setVideoAttributes(video);
//			attrs.setAudioAttributes(audio);
			
			encoder.encode(source, target, attrs);
		} catch (InputFormatException e) {
			LogUtil.print("FFMPEtool error:" + e.getMessage());
			e.printStackTrace();
		} catch (EncoderException e) {
			LogUtil.print("FFMPEtool error:" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			LogUtil.print("FFMPEtool error:" + e.getMessage());
			e.printStackTrace();
			return ERROR_YOUCODE;
		}
		
		if (source == null || !source.exists()) {
			return ERROR_NOEXIST_sourcefile;
		} else if (target == null || !target.exists()) {
			return ERROR_NOEXIST_targetpic;
		} else {
			return ERROR_NULL;			
		}
	}
	
	public static int getPicFromVideo(String sourcefile,String targetpic) {
		
		File source = null;
		File target = null;
		
		try {
			source = new File(sourcefile);
			target = new File(targetpic);
			
			Encoder encoder = new Encoder();
		
			MultimediaInfo info = encoder.getInfo(source);
			VideoInfo vinfo = info.getVideo();
			
			VideoAttributes video = new VideoAttributes();
			video.setCodec("mjpeg");
			video.setBitRate(vinfo.getBitRate());
			video.setFrameRate((int)vinfo.getFrameRate());
			video.setSize(vinfo.getSize());
			
			EncodingAttributes attrs = new EncodingAttributes();
			attrs.setFormat("image2");
			attrs.setVideoAttributes(video);
			
			encoder.encode(source, target, attrs);
		} catch (InputFormatException e) {
			LogUtil.print("FFMPEtool error:" + e.getMessage());
			e.printStackTrace();
		} catch (EncoderException e) {
			LogUtil.print("FFMPEtool error:" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			LogUtil.print("FFMPEtool error:" + e.getMessage());
			e.printStackTrace();
			return ERROR_YOUCODE;
		}
		
		if (source == null || !source.exists()) {
			return ERROR_NOEXIST_sourcefile;
		} else if (target == null || !target.exists()) {
			return ERROR_NOEXIST_targetpic;
		} else {
			return ERROR_NULL;			
		}
	}
	
	
}
